﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERPTOOLS.Class;

namespace ERPTOOLS
{
    public partial class Check : Form
    {
        bool check_action = false;
        public Check()
        {
            InitializeComponent();
        }

        private void btexit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btconfirm_Click(object sender, EventArgs e)
        {
            if (check_action)
            {
                XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//Check", "ERPType", comboBox1.Text);
                XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//Check", "Chkcustomer", chkcustomer.Checked.ToString());
                XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//Check", "Chkemail", chkemail.Checked.ToString());
                XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//Check", "RecUser", textBox1.Text);
                XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//Check", "Recemail", textBox2.Text);
                this.Visible = false;
                Transfer tf = new Transfer();
                tf.Show();
            }
            else
            {
                MessageBox.Show("请先执行检查！");
            }
        }

        private void Check_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void Check_Load(object sender, EventArgs e)
        {
            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//PurSql").ToString() == "")
            {
                llblchkpur.LinkColor = Color.Red;
            }

            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//CutSql").ToString() == "")
            {
                llblchkcut.LinkColor = Color.Red;  
            }
            comboBox1.Text = XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERPType");
            chkcustomer.Checked = Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//Chkcustomer"));
            chkemail.Checked = Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//Chkemail"));
            textBox1.Text = XMLHelper.GetInnerTextByXpath("Configure.xml", "//RecUser").ToString();
            textBox2.Text = XMLHelper.GetInnerTextByXpath("Configure.xml", "//Recemail").ToString();
            chkemail_CheckedChanged(sender,e);
        }

        private void llblchkpur_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Sql pursqlchk = new Sql(llblchkpur);
            pursqlchk.Show();
        }

        private void llblchkcut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Sql cutsqlchk = new Sql(llblchkcut);
            cutsqlchk.Show();
        }

        private void chkcustomer_CheckedChanged(object sender, EventArgs e)
        {
            if (!chkcustomer.Checked)
            {
                llblchkcut.Enabled = false ;
            }
            else
            {
                llblchkcut.Enabled = true ;
            }

        }

        private void chkemail_CheckedChanged(object sender, EventArgs e)
        {
            if (chkemail.Checked)
            {
                textBox1.BackColor = Color.Yellow;
                textBox2.BackColor = Color.Yellow;
                textBox1.Enabled = true;
                textBox2.Enabled = true;
            }
            else
            {
                textBox1.BackColor = Color.White;
                textBox2.BackColor = Color.White;
                textBox1.Enabled = false;
                textBox2.Enabled = false;
            }
        }

        private void btcheck_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            if (Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//Chkemail")))
            {
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//RecUser") == "")
                {
                    MessageBox.Show("接收人员姓名 不可为空");
                    return;
                }
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//Recemail") == "")
                {
                    MessageBox.Show("请正确配置 接收异常邮箱");
                    return;
                }
            }
            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//PurSql") == "")
            {
                MessageBox.Show("请先配置 维护供应商检查SQL");
                return;
            }
            dataGridView1.DataSource = DoCheck.CheckGYS(XMLHelper.GetInnerTextByXpath("Configure.xml", "//PurSql"), "SELECT RTRIM(SYSMA.MA002) AS GYSQC,ME006 AS GYSBH,ME002 AS QYBH FROM SCMME LEFT JOIN SYSMA ON SCMME.ME002=SYSMA.MA001 WHERE ME001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' and ME005='1'");
            if (Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//Chkcustomer")))
            {
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//CutSql") == "")
                {
                    MessageBox.Show("请先配置 维护客户检查SQL");
                    return;
                }
                dataGridView2.DataSource = DoCheck.CheckKH(XMLHelper.GetInnerTextByXpath("Configure.xml", "//CutSql"), "SELECT RTRIM(SYSMA.MA003) AS KHQC,MG006 AS KHBH,MG002 AS QYBH FROM SCMMG LEFT JOIN SYSMA ON SCMMG.MG002=SYSMA.MA001 WHERE MG001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' and MG005='1'");
            }
            check_action = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MailHelper.MailSend("smtp.21cn.com", "pupei", "pupei781024", "pupei@21cn.com", textBox1.Text, textBox2.Text, "OpenSCM供应商与客户异常检测结果", GetHtmlString("abc", false, DoCheck.CheckGYS(XMLHelper.GetInnerTextByXpath("Configure.xml", "//PurSql"), "SELECT RTRIM(SYSMA.MA002) AS GYSQC,ME006 AS GYSBH,ME002 AS QYBH FROM SCMME LEFT JOIN SYSMA ON SCMME.ME002=SYSMA.MA001 WHERE ME001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' and ME005='1'"), DoCheck.CheckKH(XMLHelper.GetInnerTextByXpath("Configure.xml", "//CutSql"), "SELECT RTRIM(SYSMA.MA003) AS KHQC,MG006 AS KHBH,MG002 AS QYBH FROM SCMMG LEFT JOIN SYSMA ON SCMMG.MG002=SYSMA.MA001 WHERE MG001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' and MG005='1'")));
        }

        public string GetHtmlString(string ExportFileName, bool isPrint, DataTable tbl,DataTable tbl1)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<HTML><HEAD>");
            sb.Append("<title>" + ExportFileName + "</title>");
            sb.Append("<META HTTP-EQUIV='content-type' CONTENT='text/html; charset=GB2312'> ");
            sb.Append("<script language=javascript>");
            sb.Append("self.resizeBy(0,0);");
            sb.Append("self.resizeTo(screen.availWidth,screen.availHeight);");
            sb.Append("</script>");
            sb.Append("<style type=text/css>");
            sb.Append("td{font-size: 9pt;border:solid 1 #000000;}");
            sb.Append("table{padding:3 0 3 0;border:solid 1 #000000;margin:0 0 0 0;BORDER-COLLAPSE: collapse;}");
            sb.Append("</style>");
            sb.Append("</HEAD>");
            if (!isPrint)
                sb.Append("<BODY  >");
            else
                sb.Append("<BODY   onload = 'window.print()'>");
            sb.Append("尊敬的"+ textBox1.Text + "：<br> 您好！<br> OpenSCM供应链管理平台(www.OpenSCM.net)智能接口工具对贵司基础资料<span style='color:red;'>异常检测结果如下</span>，还请及时进行调整，谢谢！<br><br> <span style='color:red;'>供应商检测结果:</span><br><br>");
            //sb.Append("");
            sb.Append("<table cellSpacing='0' cellPadding='0' width ='100%' border='1'");
            sb.Append(">");
            sb.Append("<tr valign='middle' bgcolor=aqua>");
            //sb.Append("<td><b>" + CommonUI.Translate("RowSequences") + "</b></td>");
            foreach (DataColumn column in tbl.Columns)
            {
                sb.Append("<td><b><span>" + column.ColumnName + "</span></b></td>");
            }
            sb.Append("</tr>");
            int iColsCount = tbl.Columns.Count;
            int rowsCount = tbl.Rows.Count - 1;
            for (int j = 0; j <= rowsCount; j++)
            {
                //sb.Append("<tr>");
                //sb.Append("<td>" + ((int)(j + 1)).ToString() + "</td>");
                for (int k = 0; k <= iColsCount - 1; k++)
                {
                    sb.Append("<td");
                    sb.Append(">");
                    object obj = tbl.Rows[j][k];
                    if (obj == DBNull.Value)
                    {
                        // 如果是NULL则在HTML里面使用一个空格替换之  
                        obj = "&nbsp;";
                    }
                    if (obj.ToString() == "")
                    {
                        obj = "&nbsp;";
                    }
                    string strCellContent = obj.ToString().Trim();
                    sb.Append("<span>" + strCellContent + "</span>");
                    sb.Append("</td>");
                }
                sb.Append("</tr>");
            }
            sb.Append("</TABLE><br><br><br>");
            if (chkcustomer.Checked)
            {
                sb.Append("<span style='color:red;'>客户检测结果:</span><br><br>");
                sb.Append("<table cellSpacing='0' cellPadding='0' width ='100%' border='1'");
                sb.Append(">");
                sb.Append("<tr valign='middle' bgcolor=aqua>");
                //sb.Append("<td><b>" + CommonUI.Translate("RowSequences") + "</b></td>");
                foreach (DataColumn column in tbl1.Columns)
                {
                    sb.Append("<td><b><span>" + column.ColumnName + "</span></b></td>");
                }
                sb.Append("</tr>");
                iColsCount = tbl1.Columns.Count;
                rowsCount = tbl1.Rows.Count - 1;
                for (int j = 0; j <= rowsCount; j++)
                {
                    //sb.Append("<tr>");
                    //sb.Append("<td>" + ((int)(j + 1)).ToString() + "</td>");
                    for (int k = 0; k <= iColsCount - 1; k++)
                    {
                        sb.Append("<td");
                        sb.Append(">");
                        object obj = tbl1.Rows[j][k];
                        if (obj == DBNull.Value)
                        {
                            // 如果是NULL则在HTML里面使用一个空格替换之  
                            obj = "&nbsp;";
                        }
                        if (obj.ToString() == "")
                        {
                            obj = "&nbsp;";
                        }
                        string strCellContent = obj.ToString().Trim();
                        sb.Append("<span>" + strCellContent + "</span>");
                        sb.Append("</td>");
                    }
                    sb.Append("</tr>");
                }
                sb.Append("</TABLE>");
            }
            sb.Append("</BODY></HTML>");
            return sb.ToString();
        }  
    }
}
